package com.zone.vehicle.superApps.view.home.recommend.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.R;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;


public class RecommendAppAdapter extends RecyclerView.Adapter<RecommendAppAdapter.ViewHolder> {

    private List<AppEntity> appList;

    private View.OnClickListener listener;

    public RecommendAppAdapter(List<AppEntity> appList, View.OnClickListener listener){
        this.appList = appList;
        this.listener = listener;
    }

    public void setAppList(List<AppEntity> appList) {
        this.appList = appList;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.fragment_recommond_item, parent, false);
        v.setOnClickListener(listener);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        AppEntity app = appList.get(position);
        if (ObjectUtils.isNotEmpty(app)){
            Glide.with(holder.itemView.getContext()).load(app.getIcon()).into(holder.icon);
//            holder.icon.setImageResource(R.drawable.ic_launcher_foreground);
            holder.name.setText(app.getName());
//            holder.star.setImageResource(getStarId(Integer.parseInt(app.getScore())));
            holder.score.setText(app.getScore());
            if (StringUtils.isNotEmpty(app.getLabels())){
                String[] labels = app.getLabels().split(",");
                if (StringUtils.isNotEmpty(labels[0])){
                    holder.tag1.setText(labels[0]);
                }
                if (StringUtils.isNotEmpty(labels[1])){
                    holder.tag2.setText(labels[1]);
                }
                if (StringUtils.isNotEmpty(labels[2])){
                    holder.tag3.setText(labels[2]);
                }
            }
//            holder.tag2.setText(app.getTags().get(1));
//            holder.tag3.setText(app.getTags().get(2));
            holder.description.setText(app.getComment());
//            binding.getRoot().setOnClickListener(listener);
            holder.itemView.setTag(app);
        }
    }

    private int getStarId(int star){
        switch (star){
            case 1:
                return R.mipmap.icon_star;
            case 2:
                return R.mipmap.icon_star;
            case 3:
                return R.mipmap.icon_star;
            case 4:
                return R.mipmap.icon_star;
            case 5:
                return R.mipmap.icon_star;
            default:
                return R.mipmap.icon_star;
        }
    }

    @Override
    public int getItemCount() {
        return appList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView icon;
        private final TextView name;
        private final ImageView star;
        private final TextView score;
        private final TextView tag1;
        private final TextView tag2;
        private final TextView tag3;
        private final TextView description;


        public ViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
            star = itemView.findViewById(R.id.star);
            score = itemView.findViewById(R.id.score);
            tag1 = itemView.findViewById(R.id.tag1);
            tag2 = itemView.findViewById(R.id.tag2);
            tag3 = itemView.findViewById(R.id.tag3);
            description = itemView.findViewById(R.id.description);
        }
    }

}
